package luogu;

import java.util.Scanner;

public class Main1706 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        generatePermutations(n);
    }

    private static void generatePermutations(int n) {
        boolean[] used = new boolean[n + 1]; // 标记数字是否已使用
        int[] permutation = new int[n]; // 当前排列
        generate(permutation, used, 0, n);
    }

    private static void generate(int[] permutation, boolean[] used, int index, int n) {
        if (index == n) {
            // 输出当前排列，每个数字保留5个场宽
            for (int num : permutation) {
                System.out.printf("%5d", num);
            }
            System.out.println();
            return;
        }

        for (int num = 1; num <= n; num++) {
            if (!used[num]) {
                used[num] = true;
                permutation[index] = num;
                generate(permutation, used, index + 1, n);
                used[num] = false; // 回溯
            }
        }
    }
}
